const orderFacade = require('../../app/facade/order.facade.js'); 
const _  =  require('lodash');

module.exports = {
    addOrder: addOrder,
    receiveOrder: receiveOrder,
    refuseOrder: refuseOrder,
    deliverOrder: deliverOrder,
    getOrderByMerchant: getOrderByMerchant, 
    getOrderById: getOrderById,
    getUnreceivedOrder: getUnreceivedOrder,
    getReceivedOrder: getReceivedOrder,
    getRefusedOrder: getRefusedOrder,
    getDeliveringOrder: getDeliveringOrder,
    getFinishedOrder: getFinishedOrder, 
}

function addOrder(req, res, next){
    let order = req.body;
    order.ostate = "0";
    order.otime = new Date();
    orderFacade.addOrder(order,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }        
    });
}

//接受订单
function receiveOrder(req, res, next){
    let order = req.body;
    order.ostate = "1";
    orderFacade.updateOrder(order,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }        
    });
}

//拒接订单
function refuseOrder(req, res, next){
    let order = req.body;
    order.ostate = "2";
    orderFacade.updateOrder(order,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }        
    });
} 

//配送订单
function deliverOrder(req, res, next){
    let order = req.body;
    order.ostate = "3";
    orderFacade.updateOrder(order,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }        
    });
}    
    

function getOrderByMerchant(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getOrderByMerchant(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}

function getOrderById(req, res, next){
    let _id = req.params._id;
    orderFacade.getOrderById(_id,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }         
    });
}

function getUnreceivedOrder(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getUnreceivedOrder(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}

function getReceivedOrder(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getReceivedOrder(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}

function getRefusedOrder(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getRefusedOrder(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}

function getDeliveringOrder(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getDeliveringOrder(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}


function getFinishedOrder(req, res, next){
    let icnum = req.params.icnum;
    orderFacade.getFinishedOrder(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }  
    });
}